package com.atguigu.yygh.oss.service.impl;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.atguigu.yygh.oss.service.OssConstantProperties;
import com.atguigu.yygh.oss.service.OssService;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.UUID;

@Service
public class OssServiceImpl implements OssService {
    @Override
    public String upload(MultipartFile file) {
        OSS ossClient=null;
        try {
        // Endpoint以华东1（杭州）为例，其它Region请按实际情况填写。
      //  String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
          String endpoint = "https://"+ OssConstantProperties.ENDPOINT;

        // 从环境变量中获取访问凭证。运行本代码示例之前，请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();

        // 填写Bucket名称，例如examplebucket。
      // String bucketName = "yygh2023812";
        String bucketName = OssConstantProperties.BUCKET;

        //上传的文件存储在bucket中的哪个目录下还有文件名
        // 填写Object完整路径，完整路径中不能包含Bucket名称，例如exampledir/exampleobject.txt。
            String dir = new DateTime().toString("yyyy/MM/dd");
            //             目录                文件名
        String objectName = dir+"/"+ UUID.randomUUID().toString().replaceAll("-","")+".png";
        //文件夹路径2023/08/12


        // 填写本地文件的完整路径，例如D:\\localpath\\examplefile.txt。
        // 如果未指定本地路径，则默认从示例程序所属项目对应本地路径中上传文件流。
      //  String filePath= "E:\\zhuomian\\1.png";

        // 创建OSSClient实例。
        ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);


            //InputStream inputStream = new FileInputStream(filePath);

            InputStream inputStream= file.getInputStream();

            // 创建PutObjectRequest对象。
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, inputStream);
            // 创建PutObject请求。
            PutObjectResult result = ossClient.putObject(putObjectRequest);

            String url="https://"+OssConstantProperties.BUCKET+"."+OssConstantProperties.ENDPOINT+"/"+objectName;
            return url;

        } catch (Exception oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
        }finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        //               协议    Bucket名称           域名                  Bucket下的目录以及文件名
       // String url="https://yygh2023812.oss-cn-hangzhou.aliyuncs.com/2023/08/12ae44b65922d945ad8d471163c01dc229.png";

        return "";
    }
}
